import { reactive, ref, toRefs } from "vue"

type paginationParams = {
    pageNum: number;
    pageSize: number;
};

export default function useTable<T extends { list: any[], total: number }, P extends paginationParams = paginationParams>(fn: (paramas: P) => RES<T>, searchParams?: Object) {
    // 分页的参数
    const paramas = reactive<paginationParams>({
        pageNum: 1,
        pageSize: 10
    });

    const list = ref<T["list"]>([]);
    const total = ref(0);

    const fetch = () => {
        fn({ ...(paramas as P), ...searchParams }).then(res => {
            if (res.code === 200) {
                list.value = res.data.list;
                total.value = res.data.total;
            };
        });
    };
    fetch();

    const pageChangeHandle = () => {
        fetch();
    };
    const pageSizeChangeHandle = () => {
        fetch();
    };
    const searchHandle = () => {
        fetch();
    };

    return {
        list,
        total,
        ...toRefs(paramas),
        pageChangeHandle,
        pageSizeChangeHandle,
        searchHandle
    }
}